import os

import xlrd


# 列表应该从DB中读取表格获取
stragety_list = ['RG MAX', 'RG MIN', r'午盘逢低买入T+1', r'振幅可能大于3%']
basedir = os.path.abspath(os.path.dirname(__file__))
fullfilename = os.path.join(basedir, '/../data.sqlite')
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + fullfilename


def test():
    # print('absdir=%s' % basedir)
    # file_dir = os.path.join(basedir, '..\\datasource\\')
    # print('file_dir=%s' % file_dir)
    file_dir = os.path.join(basedir, '..\\datasource\\')
    fullfilename = os.path.join(file_dir, '2017.12.28午盘逢低买入T+1.xlsx')
    excel_workbook = xlrd.open_workbook(fullfilename)
    excel_sheet = excel_workbook.sheets()[0]
    title = excel_sheet.cell(0, 0).value
    print(title)
    print(title[0:0])
    print(title[0:1])


def read_excel_file():
    file_dir = os.path.join(basedir, '..\\datasource\\')
    print(file_dir)
    file_list = os.listdir(file_dir)
    all_stragety_list = []
    for single_file in file_list:
        print(single_file)
        fullfilename = os.path.join(file_dir, single_file)
        # 找到Excel文件
        if '.xls' in single_file and '~$' not in single_file:
            stragety_list = []
            print('正在处理Excel文件：%s...' % fullfilename)
            excel_workbook = xlrd.open_workbook(fullfilename)
            excel_sheet = excel_workbook.sheets()[0]
            print('当前表格名称：%s' % excel_sheet.name)
            for col in range(excel_sheet.ncols):
                stragety_content = get_stragety_content(excel_sheet, col)
                stragety_list.append(stragety_content)
                # if n == 0:
                #     exec_stragety_rgmax(stragety_content)
                # elif n == 1:
                #     exec_stragety_rgmin(stragety_content)
                # elif n == 2:
                #     exec_stragety_buy_in_afternoon(stragety_content)
                # elif n == 3:
                #     exec_stragety_range(stragety_content)
            all_stragety_list.append(stragety_list)
    return all_stragety_list


#按列输出策略列表
def get_stragety_content(excel_sheet,col):
    stragety_content = []
    stragety_title = excel_sheet.cell(0, col).value
    print('当前列头单元格(0, %s)内容：%s' % (col, stragety_title))
    # n = locate_stragety(cell_title)
    for row in range(0, excel_sheet.nrows):
        cell_content = excel_sheet.cell(row, col).value
        if cell_content:
            stragety_content.append(cell_content)
    # print('策略内容共 %s 项：%s' % (len(stragety_content), stragety_content))
    return stragety_content


def exec_stragety_buy_in_afternoon(stragety_content):
    if stragety_content:
        #入库
        _str = ('正在按照 [午盘逢低买入T+1] 策略处理，共 %s 项： %s'
                '' % (len(stragety_content), stragety_content))
        print(_str)


def exec_stragety_range(stragety_content):
    if stragety_content:
        _str = ('正在按照 [振幅可能大于3%%] 策略处理，共 %s 项： %s'
                '' % (len(stragety_content), stragety_content))
        print(_str)


def exec_stragety_rgmin(stragety_content):
    if stragety_content:
        _str = ('正在按照 [RG MIN] 策略处理，共 %s 项： %s'
                '' % (len(stragety_content), stragety_content))
        print(_str)


def exec_stragety_rgmax(stragety_content):
    if stragety_content:
        _str = ('正在按照 [RG MAX] 策略处理，共 %s 项： %s'
                '' % (len(stragety_content), stragety_content))
        print(_str)


def locate_stragety(title_str):
    for i in range(len(stragety_list)):
        if stragety_list[i] in title_str:
            print('找到第 %s 号策略：%s。' % (i, stragety_list[i]))
            return i
    else:
        print('没有找到在策略列表中找到 "%s"。' % title_str)
        return -1


if __name__ == '__main__':
    test()